﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NetSurveillance.WebApp.DataServices.Models;
using ServiceStack.OrmLite;

namespace NetSurveillance.WebApp.DataServices.Implementations
{
    public class KeywordService : BaseService<Keyword>
    {
        public KeywordService(string connStr, IOrmLiteDialectProvider provider = null)
            : base(connStr, provider)
        {
        }

        public KeywordTemplate GetKeywordTemplateById(long id)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                var keywordTemplate = db.IdOrDefault<KeywordTemplate>(id);
                return keywordTemplate;
            }
        }

        public IEnumerable<KeywordTemplate> GetAllKeywordsInTemplate(bool ignoreDeleted = false,
                                                                     Expression<Func<KeywordTemplate, bool>> predicate = null)
        {
            if (predicate == null)
                predicate = arg => true;

            using (var db = DbFactory.OpenDbConnection())
            {
                return ignoreDeleted
                           ? db.Select(predicate)
                           : db.Select(predicate.And(p => p.StatusToken != 0));
            }
        }

        public ServiceResult SaveKeywordToTemplate(KeywordTemplate keywordTemplate)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                if (keywordTemplate.Id <= 0)
                {
                    var oldEntry =
                        db.SingleOrDefault<KeywordTemplate>("\"KeywordName\"={0}".Params(keywordTemplate.KeywordName));
                    if (oldEntry != null)
                    {
                        oldEntry.StatusToken = 1;
                        oldEntry.LastModifiedTime = DateTime.Now;
                    }
                    else
                    {
                        oldEntry = keywordTemplate;
                    }
                    db.Save(oldEntry);
                }
                else
                    db.Save(keywordTemplate);
            }
            return ServiceResult.SuccessResult;
        }

        public ServiceResult DeleteKeywordFromTemplate(long id)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                var model = db.IdOrDefault<KeywordTemplate>(id);
                if (model == null) return new ServiceResult { ExecutedSuccess = false, Message = "该条数据不存在数据库" };
                model.StatusToken = 0;
                db.Save(model);
            }
            return ServiceResult.SuccessResult;
        }

        public IList<DataKeywordHost> GetHitKeywordInfoHosts(long taskId)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                const string searchSql =
                    @"select distinct DR.""Id"" as ""DRId"",Kw.""Id"" as ""KeywordId"",Hst.* from ""DataRecordKeywords"" as DRK 
                        left join ""DataRecords"" as DR on DRK.""DataRecordId"" = DR.""Id""
                        left join ""Hosts"" as Hst on DR.""HostId""=Hst.""Id""
                        left join ""Keywords"" as Kw on DRK.""KeywordId"" = Kw.""Id""
                        where DRK.""StatusToken"" != 0 and DR.""TaskId"" = :taskId";
                var keywordInfoHosts = db.SqlList<DataKeywordHost>(searchSql, new { taskId });
                return keywordInfoHosts;
            }
        }

        public IList<DataKeywordHost> GetHitKeywordInfoHostsByCaseId(long caseId)
        {
            using (var db = DbFactory.OpenDbConnection())
            {
                const string searchSql =
                    @"select distinct DR.""Id"" as ""DRId"",Kw.""Id"" as ""KeywordId"",
                        Kw.""KeywordName"",Hst.* from ""DataRecordKeywords"" as DRK 
                        left join ""DataRecords"" as DR on DRK.""DataRecordId"" = DR.""Id""
                        left join ""Hosts"" as Hst on DR.""HostId""=Hst.""Id""
                        left join ""Keywords"" as Kw on DRK.""KeywordId"" = Kw.""Id""
                        where DRK.""StatusToken"" != 0 and DR.""TaskId"" in 
                        (select ""Id"" from ""Tasks"" where ""CaseId""=:caseId)";
                var keywordInfoHosts = db.SqlList<DataKeywordHost>(searchSql, new { caseId });
                return keywordInfoHosts;
            }
        } 
    }
}